/**
 * 
 */
package org.log4jeditor.xml.editor.validation;

import static org.junit.Assert.assertEquals;

import org.eclipse.core.resources.IResource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.log4jconfig.xml.editor.validation.ContentValidator;
import org.log4jeditor.xml.editor.AbstractUITestCase;
import org.log4jeditor.xml.editor.DirtiesProjectFiles;

/**
 * @author Xi
 * 
 */
public class LoggerContentValidatorTestCase extends AbstractUITestCase {

	private ContentValidator validator;

	@Before
	public void setup() throws Exception {
		setupEditorConfiguration("log4j-validation-logger.xml");
		// remove all markers on document
		file.deleteMarkers(ContentValidator.DEFAULT_MARKER_ID, true, IResource.DEPTH_INFINITE);
		file.deleteMarkers(ContentValidator.DEPRECATED_ELEMENT_MARKER_ID, true,IResource.DEPTH_INFINITE) ;
		assertEquals("Wrong number of markers:", 0, getMarkers().length);

		validator = new ContentValidator();
		validator.connect(document);
	}

	@After
	public void tearDown() throws Exception {
		validator.disconnect(document);
		super.tearDownProject();
	}

	@Test
	public void testScanAndValidateValidLoggerName() throws Exception {
		int offset = getDocumentOffset(25, 19, "logger");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	public void testScanAndValidateInvalidLoggerName() throws Exception {
		int offset = getDocumentOffset(29, 19, "logger");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateEmptyLoggerName() throws Exception {
		int offset = getDocumentOffset(22, 19, "logger");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateMissingLoggerName() throws Exception {
		int offset = getDocumentOffset(37, 9, "logger");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateNewInvalidLoggerName() throws Exception {
		int offset = getDocumentOffset(25, 19, "logger");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "test".length(), "unknownLogger");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateFixInvalidLoggerNameWithPackageName() throws Exception {
		int offset = getDocumentOffset(29, 19, "logger");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "unknownLogger".length(), "test");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateFixInvalidLoggerNameWithClassName() throws Exception {
		int offset = getDocumentOffset(29, 19, "logger");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "unknownLogger".length(), "test.TestLayout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateKeepInvalidLoggerName() throws Exception {
		int offset = getDocumentOffset(29, 19, "logger");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "unknownLogger".length(), "test.unknownLogger");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateKeepValidLoggerName() throws Exception {
		int offset = getDocumentOffset(25, 19, "logger");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "test.TestAppender".length(), "test.TestLayout");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

}
